package com.ecut.property_project.controller;

import com.ecut.property_project.model.character.Account;
import com.ecut.property_project.model.character.Role;
import com.ecut.property_project.model.character.User;
import com.ecut.property_project.service.impl.UserServiceImpl;
import com.ecut.property_project.utils.QiniuUtils;
import com.ecut.property_project.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping(value="/getById" ,produces="application/json;charset=utf-8")
    public Result findUserById(){
        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        User user = userService.findUserById(account.getId());
        return Result.succ(200,"success",user);
    }

    @RequiresRoles(value = "admin")
    @RequestMapping(value = "/findAllUser")
    public Result findAllUser() {
        List<User> users = userService.findAllUser();
        return Result.succ(200,"success",users);
    }

    // 注销用户接口, 前端传入用户的 id；’id‘ , 后期需要重写
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/deleteUser")
    public Result deleteUser(@RequestBody Map<String, String> data) {
        return Result.fail("注销失败");
    }

    @RequestMapping(value = "/updateUser")
    public Result updateUser(@RequestBody User user){
        Boolean flag = userService.updateUser(user);
        if(!flag) {
            return Result.fail("修改失败");
        }
        return  Result.succ(200,"success",null);
    }

    // 获取用户角色名，可能多个
    @RequestMapping(value = "/getUserRolesById")
    public Result getUserRolesById(){

        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        List<Role> roles = userService.getUserRolesById(account.getId());
        return  Result.succ(200,"success",roles);
    }

    // 用户修改图片
    @RequestMapping(value = "/updateImage")
    public Result updateImage(@RequestParam("file") MultipartFile file) throws IOException {
        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        User user = userService.findUserById(account.getId());
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        if(!user.getImageUrl().equals("image_default.png")) {
            QiniuUtils.deleteFileFromQiniu(user.getImageUrl());
        }
        String imageName = user.getId() + "," +ft.format(user.getRegister());
        Boolean flag = userService.updateUserImage(imageName, user.getId());
        if(flag){
            QiniuUtils.upload2Qiniu(file.getBytes(),imageName);
            return  Result.succ(200,"success",imageName);
        }
        return Result.fail("持久化user表错误");
    }

    // 用户根据图片名获取图片
    @RequestMapping(value = "/getImage")
    public Result updateImage() throws UnsupportedEncodingException {
        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        User user = userService.findUserById(account.getId());
        String imageName = user.getImageUrl();
        String finalUrl = QiniuUtils.getEAndToken(imageName);
        return  Result.succ(200,"success",finalUrl);
    }
}
